package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.MqOpsService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author codingyz
 * @Description
 * @date 2025/3/30 16:47
 * @Version 1.0
 */
@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;

    @Autowired
    private AlbumStatMapper albumStatMapper;

    @Autowired
    private TrackInfoService trackInfoService;

    @Override
    public int updatePaidCount(String orderNo, Long userId) {
        Result<OrderInfo> orderInfoResult = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo,userId);
        OrderInfo orderInfoData = orderInfoResult.getData();
        if (orderInfoData == null) {
            log.error("远程调用order微服务获取订单信息失败");
            throw new GuiguException(201, "远程调用order微服务获取订单信息失败");
        }
        String itemType = orderInfoData.getItemType();
        Long albumId;
        switch (itemType) {
            case "1001": // 专辑
                albumId = orderInfoData.getOrderDetailList().get(0).getItemId();
                albumStatMapper.updatePaidCount(albumId, 1);
                break;
            case "1002": // 声音
                List<OrderDetail> orderDetailList = orderInfoData.getOrderDetailList();
                Long trackId = orderDetailList.get(0).getItemId();
                // 根据声音id获取专辑
                albumId = trackInfoService.getAlbumIdByTrackId(trackId);
                albumStatMapper.updatePaidCount(albumId, orderDetailList.size());
                break;
            case "1003": // vip会员
                return 1;
        }
        return 1;
    }
}
